//
//  JHNavigationController.m
//  JHD_iOS
//
//  Created by 邓建 on 2018/7/31.
//  Copyright © 2018年 邓建. All rights reserved.
//

#import "JHNavigationController.h"

@interface JHNavigationController ()<UIGestureRecognizerDelegate>


@end

@implementation JHNavigationController

- (void)viewDidLoad {
     [super viewDidLoad];
    // 设置导航控制器为手势识别器的代理
     self.interactivePopGestureRecognizer.delegate = self;
     [self.navigationBar setBackgroundImage:[DJTool imageWithColor:JKMainColor] forBarMetrics:UIBarMetricsDefault];
}

//设置导航栏的字体颜色
+ (void)initialize
{
    UINavigationBar *bar = [UINavigationBar appearance];
    //修改标题字体颜色及大小
    [bar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
}

// 代理方法上
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(nonnull UIViewController *)viewController animated:(BOOL)animated{
    //加上
    [self.tabBarController.tabBar removeFromSuperview];
}
/**
 *  重写push方法的目的 : 拦截所有push进来的子控制器
 *
 *  @param viewController 刚刚push进来的子控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) { // 如果viewController不是最早push进来的子控制器
        // 左上角
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
        backButton.imageView.contentMode=UIViewContentModeScaleAspectFit;
        backButton.backgroundColor=[UIColor clearColor];
        backButton.frame=CGRectMake(0, 0, 60,kTopHeight);
        // 这句代码放在sizeToFit后面
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -40, 0, 0);
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        
        // 隐藏底部的工具条
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    // 所有设置搞定后, 再push控制器
    [super pushViewController:viewController animated:animated];
    
    // 修改tabBra的frame
    CGRect frame = self.tabBarController.tabBar.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
    self.tabBarController.tabBar.frame = frame;
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}


#pragma mark - <UIGestureRecognizerDelegate>
/**
 *  手势识别器对象会调用这个代理方法来决定手势是否有效
 *
 *  @return YES : 手势有效, NO : 手势无效
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // 手势何时有效 : 当导航控制器的子控制器个数 > 1就有效
    return self.childViewControllers.count > 1;
}
@end
